<!DOCTYPE html>
<html>

<head>
    <meta http-equiv="X-UA-Compatible" content="IE=9">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta content="yes" name="apple-mobile-web-app-capable">
    <meta content="yes" name="apple-touch-fullscreen">
    <meta content="telephone=no" name="format-detection">
    <meta content="black" name="apple-mobile-web-app-status-bar-style">
    <meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0" />
    <title>中国石油大学（北京）克拉玛依校区统一身份认证</title>
    <link rel="stylesheet" href="/static/themes/sso_cupk/css/index.css" />
    <link rel="icon" href="https://cup.edu.cn/images/favicon.ico" />
    <script src="/static/js/wwLogin-1.0.0.js"></script>
</head>
<style>
    input::-ms-clear, input::-ms-reveal { display: none; }
</style>

<body onload="changeLoginWay('login')">
<div class="wrap">
    <div class="logo"></div>
    <div class="loginCont">
        <div class="loginTable">
            <img src="/static/themes/sso_cupk/images/logo.png" style="margin-top: 20px; width: 100%;">
            <h1>统一身份认证</h1>
            <h2 class='comeSoon'>即将登录 → 管道评估系统</h2>
            <div class="loginToggle">
                <a href="javascript:void(0);" id="account" class="login-select" onclick="changeLoginWay('login')">登录<b></b></a>
                <a href="javascript:void(0);" id="phone" class="login-select" onclick="changeLoginWay('signup')">注册<b></b></a>
            </div>
            <div class="account-table">
                <form method="post" id="fm1" class="loginForm" action="/user/login">
                    <input type="hidden" name="_eventId" value="submit" />
                    <input type="hidden" name="geolocation" />
                    <input type="hidden" id="loginType" name="loginType" value="username">
                    <input type="hidden" id="userLoginSystem" name="system" value="27A5A4DF0C874122A0AFE0367F0A3F46">
                    <input type="hidden" id="enableCaptcha" name="enableCaptcha" value="Y">
                    <div class="account-toggle" style="display: block;" id="login">
                        <table class="account" cellpadding="0" cellspacing="0">
                            <tr>
                                <td>
                                    <input class="userpass userName" name="username" id="username" type="text" placeholder="请输入用户名" value="">
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <input class="userpass password" name="password" id="password" type="password" placeholder="请输入密码" onfocus="this.placeholder=''" onblur="this.placeholder='请输入密码'">
                                </td>
                            </tr>
                            <tr>
                                <td class="passError" id="msg"></td>
                            </tr>
                            <tr>
                                <td class="login-button">
                                    <input class="userButton" type="submit" value="登录">
                                </td>
                            </tr>
                            <tr>
                                <td class="remember">
                                    <a href="/static/js/pwd.html" id="J_fogetUserPwd2">忘记密码 ？</a>
                                </td>
                            </tr>
                        </table>
                    </div>
                    <!--用户注册-->
                    <div class="account-toggle" id="signup">
                        <table class="account" cellpadding="0" cellspacing="0">
                            <tr>
                                <td>
                                    <input class="userpass userName" name="username" id="signup-username" type="text" placeholder="请输入用户名" required>
                                    <span id="usernameError" style="color: red;"></span>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <input class="userpass password" name="password" id="signup-password" type="password" placeholder="请输入密码" oninput="checkPasswordStrength()" required>
                                    <span id="passwordError" style="color: red;"></span>
                                </td>
                            </tr>
                            <tr>
                                <td class="login-button">
                                    <input class="userButton" type="button" value="注册" onclick="registerUser()">
                                </td>
                            </tr>
                        </table>
                    </div>
                </form>
            </div>
        </div>
    </div>
    <div class="footer">
        <p></p>
    </div>
</div>
<script src="/static/themes/common/jquery-3.7.1.min.js"></script>
<script src="/static/themes/common/jquery.cookie.js"></script>
<script src="/static/themes/sso_cupk/js/casLoginView.js"></script>
<script src="/static/themes/sso_cupk/js/flexible.js"></script>
<script src="/static/themes/sso_cupk/js/Base64Utils.js"></script>
<script src="/static/themes/common/jsencrypt.js"></script>
<script>
    function checkPasswordStrength() {
        const password = document.getElementById('signup-password').value;
        const passwordError = document.getElementById('passwordError');

        const strongRegex = new RegExp("^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#\\$%\\^&\\*])(?=.{8,})");

        if (!strongRegex.test(password)) {
            passwordError.textContent = "密码强度不足，请包含大小写字母、数字和特殊字符，且长度至少为8位";
        } else {
            passwordError.textContent = "";
        }
    }

    async function registerUser() {
        const username = document.getElementById('signup-username').value;
        const password = document.getElementById('signup-password').value;
        const usernameError = document.getElementById('usernameError');
        const passwordError = document.getElementById('passwordError');

        usernameError.textContent = "";
        passwordError.textContent = "";

        const checkResponse = await fetch(`/user/check?username=${username}`);
        const isTaken = await checkResponse.json();

        if (isTaken) {
            usernameError.textContent = "用户名已存在";
            return;
        }

        checkPasswordStrength();
        if (passwordError.textContent !== "") {
            return;
        }

        const response = await fetch('/user/register', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({ username, password })
        });

        if (response.redirected) {
            window.location.href = response.url;
        } else {
            const result = await response.text();
            alert(result);
        }
    }

    $(document).ready(function() {
        var str = window.location.search;
        var i1 = str.indexOf("/");
        var opt2 = str.indexOf("/", str.indexOf("/", str.indexOf("/") + 1) + 1);
        var a = str.substring(i1 + 2, opt2);
    })
</script>
</body>

</html>
